/**
 * Copyright © 2012, 2013 dr. ir. Jeroen M. Valk
 *
 * This file is part of ComPosiX.
 *
 * ComPosiX is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * ComPosiX is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ComPosiX. If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, const char* argv[])
{
	int fd[2], n;
	if (argc != 3) {
		fprintf(stderr, "%s: usage: composix <integer> <filename>\n", argv[0]);
		fprintf(stderr, "   where: <integer> is the number of pipes to be created\n");
		fprintf(stderr,"      and <filename> is the name of the shell script to be executed\n");
		return 1;
	}

	n = atoi(argv[1]);
	for (int i=0; i<n; ++i) {
		pipe(fd);
		if (fd[0]!=3+i*2 || fd[1]!=4+i*2) {
			fprintf(stderr, "%s: open file descriptors exist; close them before running composix\n", argv[0]);
			return 1;
		}
	}
		
	execl("/bin/bash","bash","--posix",argv[2], (char *) NULL);
}

